package QualificationRound.XorSum;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Bate1
{
	private static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args)
	{
		int i = sc.nextInt();
		for(int j=1; j<i; j++)
		{
			System.out.println("Case #" + j + ":");
			next();
		}
	}

	private static void next()
	{
		int m=sc.nextInt();
		int n=sc.nextInt();
		
		boolean[][] nums = new boolean[m][32];
		int[] lnums = new int[m];
		
		for(int i=0; i<m; i++)
		{
			lnums[i] = sc.nextInt();
			char[] num = Integer.toBinaryString(lnums[i]).toCharArray();
			
			for(int j=num.length-1,k=0; j>-1; j--,k++)
			{
				nums[i][k] = (num[j] == '1');
			}
		}
		
		for(int i=0; i<n; i++)
		{
			char[] num = Integer.toBinaryString(sc.nextInt()).toCharArray();
			
			List<Integer> clnums = new LinkedList<Integer>();
			
			for(int t : lnums)
			{
				clnums.add(t);
			}
			
			for(int j=num.length-1, k=0; j>0; j--, k++)
			{
				if(num[k] == '0')
					continue;
				
				
				
				if(clnums.size() == 1)
				{
					System.out.println(clnums.get(0));
					return;
				}
			}
		}
	}

}
